<template>
  <div>
    <v-edit :title="init?.isAdd?'二级分类新增':'二级分类编辑'" :init="comm" :infos="info" @reset="reset" @save="save"
            @close="close" :rules="rules">
      <template #formItem>
        <div class="two-col intro-y">
          <wde-select label="一级分类名称" prop="cateMainKey" v-model="info.cateMainKey" :options="mainList"/>
        </div>
        <div class="two-col intro-y">
          <wde-input label="二级分类编码" prop="cateSubCode" v-model="info.cateSubCode"/>
        </div>
        <div class="one-col intro-y">
          <wde-input label="二级分类名称" prop="cateSubName" v-model="info.cateSubName"/>
        </div>
        <div class="one-col intro-y">
          <wde-input label="范畴" prop="scope" v-model="info.scope" type="textarea"/>
        </div>
        <div class="one-col intro-y">
          <wde-input label="参考" prop="consult" v-model="info.consult" type="textarea"/>
        </div>
        <div class="one-col intro-y">
          <wde-input label="备注" prop="remark" v-model="info.remark" type="textarea"/>
        </div>
        <!-- <div class="two-col intro-y">
          <wde-price label="金额(万元)" v-model="info.price" />
        </div>
        <div class="two-col intro-y">
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-select label="项目类型" v-model="info.type" :options="options" />
          <wde-muti-select label="等级" v-model="info.levels" :options="options" />
        </div>
        <div class="two-col intro-y">
          <wde-date label="计划开始时间" dateType="date" v-model="info.date" />
        </div>
        <div class="two-col intro-y">
          <wde-switch label="是否激活" v-model="info.isActive" />
        </div> -->
      </template>
    </v-edit>
  </div>
</template>

<script setup>
import {ref, toRefs, watch, getCurrentInstance, onMounted} from "vue";
import {useEditLogic} from "@/views/base/useEditLogic";
import * as $api from "@/api/api.js";
import {ElMessage} from 'element-plus'
import {
  CategoryAddMain, CategoryAddSub, CategoryReferMain, CategoryUpdateMain, CategoryUpdateSub,
  EquipmentInsert,
  EquipmentUpdate,
  MobileRegInsert,
  MobileRegUpdate,
  ReferDict
} from "@/api/api.js";

const {proxy} = getCurrentInstance();
const emit = defineEmits(["close"]);

const props = defineProps({
  args: {
    type: Object,
    default: () => null,
  },
  init: {
    type: Object,
    default: () => null,
  },
});
// 使用 toRefs 解构 props
const {init} = toRefs(props);
const info = ref({
  cateMainKey: "",
  cateSubKey: "",
  cateSubCode: "",
  cateSubName: "",
  scope: "",
  consult: "",
  remark: ""
});
const userKey = ref(localStorage.getItem("userKey"));
const mainList = ref([]);
const rules = ref({
  cateMainKey: [
    {required: true, message: "请选择一级分类", trigger: "blur"},
  ],
  cateSubCode: [
    {required: true, message: "请输入编码", trigger: "blur"},
  ],
  cateSubName: [
    {required: true, message: "请输入名称", trigger: "blur"},
  ]
});
// onMounted(
//     () => {
//       getMainList();
//     }
// )
const getMainList = () => {
  $api.CategoryReferMain({}).then((res) => {
    mainList.value = res.map(e => {
      return {
        value: e.cateMainKey,
        label: e.cateMainName,
      };
    });
  });
};
const buildAdd = () => {
  return $api.CategoryAddSub(info.value);
};

const buildUpdate = () => {
  return $api.CategoryUpdateSub(info.value);
};
const resetAdd = () => {
  proxy.$propsEmpty(info.value);
  // proxy.$propsCopy(info.value, props.init);
};
const resetUpdate = () => {
  proxy.$propsCopy(info.value, props.init);
};
const clearInfo = () => {
  proxy.$propsEmpty(info.value);
};

const close = () => {
  emit("close");
};
const {loading, comm, isView, reset, save, VEdit} = useEditLogic(
    init,
    info,
    resetAdd,
    resetUpdate,
    clearInfo,
    buildAdd,
    buildUpdate,
    close
);
watch(
    () => comm,
    (newVal) => {
      console.log(newVal);
    }
);
watch(
    () => props.init,
    (newVal) => {
      if (newVal) {
        getMainList();
      }
    }
);
</script>

<style></style>